package my.jwds.p290;

import java.util.HashMap;
import java.util.Map;

public class WordPattern {

    public static void main(String[] args) {
        WordPattern obj = new WordPattern();

        System.out.println(obj.wordPattern("aba","dog cat cat"));
    }

    public boolean wordPattern(String pattern, String s) {
        Map<Character,String> map1=  new HashMap<>();
        Map<String,Character> map2 = new HashMap<>();
        String[] sarr = s.split(" ");
        if (pattern.length() != sarr.length)return false;
        for (int i = 0; i < pattern.length(); i++) {
            String s1 = map1.get(pattern.charAt(i));
            Character c1=  map2.get(sarr[i]);
            if (s1 == null && c1 != null || s1!=null && c1 == null || c1!=null && c1!=pattern.charAt(i)){
                return false;
            }
            if (s1 == null){
                map1.put(pattern.charAt(i),sarr[i]);
                map2.put(sarr[i],pattern.charAt(i));
            }
        }
        return true;
    }
}